/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import com.carnation.mong.platform.service.AnalysisService;
import com.carnation.mong.platform.service.DRelationService;
import com.carnation.mong.platform.service.DataRevisionService;
import com.carnation.mong.platform.service.DataService;
import com.carnation.mong.platform.service.DomainService;
import com.carnation.mong.platform.service.MOISession;
import com.carnation.mong.platform.service.MRelationService;
import com.carnation.mong.platform.service.ModelAttributeService;
import com.carnation.mong.platform.service.PackageService;
import com.carnation.mong.platform.service.Query;
import com.carnation.mong.platform.service.RelationTypeService;
import com.carnation.mong.platform.service.SecurityService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.ProxyService;

/**
 * @author Nick
 * 
 */
public final class MOIProxyFactory {

	/**
	 * @param item
	 * @return
	 * @throws NoSuchMethodError
	 */
	public static ProxyService getProxy(List<Item> items)
			throws NoSuchMethodError {
		String label = items.get(items.size() - 1).getLabel();
		if (match(AnalysisService.class, label)) {
			return new AnalysisServiceImpl(items);
		} else if (match(DataRevisionService.class, label)) {
			return new DataRevisionServiceImpl(items);
		} else if (match(DataService.class, label)) {
			return new DataServiceImpl(items);
		} else if (match(DomainService.class, label)) {
			return new DomainServiceImpl(items);
		} else if (match(DRelationService.class, label)) {
			return new DRelationServiceImpl(items);
		} else if (match(ModelAttributeService.class, label)) {
			return new ModelAttributeServiceImpl(items);
		} else if (match(MOISession.class, label)) {
			return new MOISessionImpl(items);
		} else if (match(MRelationService.class, label)) {
			return new MRelationServiceImpl(items);
		} else if (match(PackageService.class, label)) {
			return new PackageServiceImpl(items);
		} else if (match(Query.class, label)) {
			return new QueryImpl(items);
		} else if (match(RelationTypeService.class, label)) {
			return new RelationTypeServiceImpl(items);
		} else if (match(SecurityService.class, label)) {
			return new SecurityServiceImpl(items);
		} else {
			throw new NoSuchMethodError(items.get(items.size() - 1).getLabel());
		}
	}

	@SuppressWarnings("unchecked")
	private static boolean match(Class clas, String label) {
		return label.startsWith(clas.getSimpleName() + ".")
				|| label.startsWith(clas.getName() + ".");
	}
}
